﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
//using LuxsanProjectHelpLib.Log;

namespace LuxsanTool.Tool
{

    public delegate void LuxsanToolDelegate(string _strMsgType,string _strLogName,string _strStationName,string _strBackMsg,bool _bDisForm=false);

    public delegate void LuxsanToolAlarmDelegate(string _strMsgType, string _strErrorCode, string _strLogName,string _strStationName ,string _strBackMsg, bool _bDisForm = false);

    /// <summary>
    /// Log删除界面显示
    /// </summary>
    /// <param name="_strStation"></param>
    /// <param name="_strMessage"></param>
    public delegate void MyDelegate(string _strStation, string _strMessage);
    /// <summary>
    /// 工具界面显示
    /// </summary>
    public delegate void ToolDisDelegate();

    /// <summary>
    /// MES基础工具
    /// </summary>
    public   class BaseTool
    {
     
        /// <summary>
        /// 编辑ID 用于标记哪种工具
        /// </summary>
        public int m_EditId;

        public string m_strLogName = "Defealt";
        /// <summary>
        /// 上层工站
        /// </summary>
        public string m_strPreStationName;

        /// <summary>
        /// 工站名称
        /// </summary>
        public  string m_strStationName;

        /// <summary>
        /// 工具名称
        /// </summary>
        public string m_strToolName;

        /// <summary>
        /// 工具使能
        /// </summary>
        public bool m_bEnable;

        /// <summary>
        /// 单次运行
        /// </summary>
        public bool bSingleRun;

        /// <summary>
        /// 保存的LOG使能
        /// </summary>
        public bool m_bLogEnable;

        ///// <summary>
        ///// LOG保存到文档使能
        ///// </summary>
        //public bool m_bLogSaveFileEnable;

        /// <summary>
        /// 工具延时时间前
        /// </summary>
        public int m_iToolDelayTimeFront;

        /// <summary>
        /// 工具延时时间后
        /// </summary>
        public int m_iToolDelayTimeBack;

        /// <summary>
        /// 运行Log显示到界面
        /// </summary>
        public LuxsanToolDelegate m_LuxsanToolDelegate;

        /// <summary>
        /// 报警Log
        /// </summary>
        public LuxsanToolAlarmDelegate m_LuxsanToolAlarmDelegate;

        /// <summary>
        /// 输入值
        /// </summary>
        public Dictionary<string, ToolInputValue> m_ToolInputValue = new Dictionary<string, ToolInputValue>();

        /// <summary>
        /// 输出值
        /// </summary>
        public Dictionary<string, ToolOutPutValue> m_ToolOutPutValue = new Dictionary<string, ToolOutPutValue>();

        /// <summary>
        /// 保存文件
        /// </summary>
        public LuxsanBaseHelpLib.FileOperate.IniFile m_iniFile = new LuxsanBaseHelpLib.FileOperate.IniFile();

        /// <summary>
        /// 保存Text文件
        /// </summary>
        public LuxsanBaseHelpLib.FileOperate.TextFile m_TextFile = new LuxsanBaseHelpLib.FileOperate.TextFile();

        /// <summary>
        /// 保存文件
        /// </summary>
        public LuxsanBaseHelpLib.FileOperate.CsvFile m_csvFile = new LuxsanBaseHelpLib.FileOperate.CsvFile();

        /// <summary>
        /// Log
        /// </summary>
        public LuxsanProjectHelpLib.Log.LogFile m_LogFile = new LuxsanProjectHelpLib.Log.LogFile();

        /// <summary>
        /// 配置文件
        /// </summary>
        public string m_strFilePath = Application.StartupPath ;

        /// <summary>
        /// 执行时间
        /// </summary>
        public Stopwatch m_stopwatchStart = new Stopwatch();
        public Stopwatch m_stopwatchToolRunTimerAll = new Stopwatch();//留作测试用
        /// <summary>
        /// 执行结果
        /// </summary>
        public LuxsanBaseHelpLib.ExecuteStatus m_executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

        public void SetFilePath(string _strFilePath)
        {
            m_strFilePath = _strFilePath ;
        }

        static Mutex mutex = new Mutex();


        #region  Initial  初始化虚函数
        /// <summary>
        /// 初始化虚函数
        /// </summary>
        public virtual void Initial()
        {

        }
        #endregion

        #region Realse  释放虚函数
        /// <summary>
        /// 释放
        /// </summary>
        public virtual void Realse()
        {

        }
        #endregion

        #region SavePara  保存参数虚函数
        /// <summary>
        /// Save_Para 保存参数
        /// </summary>
        public virtual void  SavePara()
        {

        }
        #endregion

        #region SetSize 设定尺寸虚函数
        /// <summary>
        /// 设定尺寸虚函数
        /// </summary>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public virtual void SetSize(int Width, int Height)
        {

        }
        #endregion

        #region  DisRunResult 
        /// <summary>
        /// 显示运行结果
        /// </summary>
        public virtual void DisRunResult()
        {
        }
        #endregion

        #region  SaveLog 保存LOG
        /// <summary>
        ///  SaveLog 保存LOG   _bSaveAlarm保存异常信息  _bSaveOtherMark保存 PDCA Hive MES 标志
        /// </summary>
        /// <param name="_strLogMag"></param>
        /// <param name="_bSaveAlarm"></param>
        /// <param name="_bSaveOtherMark"></param>
        public virtual void SaveLog(string _strLogMag, bool _bSaveAlarm = true, bool _bSaveOtherMark = true, bool _bDisForm = false)
        {
            if (m_LuxsanToolDelegate == null)
                return;
            #region 显示到界面
            if (this.m_bLogEnable && !m_executeStatus.bStatus && _bSaveAlarm)
                m_LuxsanToolDelegate("ExceptionLog", m_strLogName, m_strStationName, _strLogMag, _bDisForm);

            if (this.m_bLogEnable)
                m_LuxsanToolDelegate("RunningLog", m_strLogName, m_strStationName, _strLogMag, _bDisForm);
            #endregion

         

        }
        #endregion

        #region Alarm 添加报警记录(委托)
        /// <summary>
        /// 添加报警记录(委托)
        /// </summary>
        /// <param name="_iErrorCode"></param>
        /// <param name="_strStationId"></param>
        /// <param name="_strRunMsg"></param>
        public virtual void SaveAlarm(string  _strErrorCode, string _strRunMsg, bool _bDisForm = false)
        {
            if (m_LuxsanToolAlarmDelegate == null)
                return;
            //AddLog(_strRunMsg);
            if (m_LuxsanToolAlarmDelegate != null)
            {
                m_LuxsanToolAlarmDelegate("Alarm", _strErrorCode, m_strLogName, m_strStationName, _strRunMsg, _bDisForm);
            }
        }
        #endregion






        #region RunTool 运行工具
        /// <summary>
        /// 运行工具
        /// </summary>
        /// <returns></returns>
        public virtual LuxsanBaseHelpLib.ExecuteStatus RunTool()
        {
            return m_executeStatus;
        }
        #endregion

        #region  AssistToolOutPutValue 分配输出结果
        /// <summary>
        /// 分配输出结果
        /// </summary>
        public virtual void AssistToolOutPutValue()
        {

        }
        #endregion

        #region  RunTool 工具运行
        /// <summary>
        /// 工具运行
        /// </summary>
        /// <param name="_strFinishedSkip"></param>
        /// <returns></returns>
        public virtual LuxsanBaseHelpLib.ExecuteStatus RunTool(out string _strFinishedSkip)
        {
            _strFinishedSkip = "";
            return  m_executeStatus;
        }
        #endregion

        #region ModifyStationConfigName 调整引用工站名(调整工站名的时候调用)
        /// <summary>
        /// 调整引用工站名(调整工站名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void ModifyStationConfigName(string _strNewStationName, string _strOldStationName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                if (toolInputValue.Value.InPutResources.GetWorkName == _strOldStationName)
                    toolInputValue.Value.InPutResources.GetWorkName = _strNewStationName;
            }
        }
        #endregion

        #region DeleteStationConfigName 删除引用工站名(调整工站名的时候调用)
        /// <summary>
        ///  删除引用工站名(调整工站名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void DeleteStationConfigName(string _strDeleteStationName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                if (toolInputValue.Value.InPutResources.GetWorkName == _strDeleteStationName)
                {
                    toolInputValue.Value.InPutResources.GetWorkName = "Null";
                    toolInputValue.Value.InPutResources.GetToolName = "";
                    toolInputValue.Value.InPutResources.GetValueName = "";
                    toolInputValue.Value.InPutResources.UsedValueName = "";
                    toolInputValue.Value.bMark = false;
                }
            }
        }
        #endregion


        #region ModifyToolConfigName 调整引用工具名(调整工具名的时候调用)
        /// <summary>
        /// 调整引用工具名(调整工具名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void ModifyToolConfigName(string _strCurrentStation, string _strNewToolName, string _strOldToolName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                if (toolInputValue.Value.InPutResources.GetWorkName == _strCurrentStation &&
                    toolInputValue.Value.InPutResources.GetToolName == _strOldToolName)
                {
                    toolInputValue.Value.InPutResources.GetToolName = _strNewToolName;

                }
            }
        }
        #endregion

        #region DeleteToolConfigName 删除引用工具名 (调整工具名的时候调用)
        /// <summary>
        /// 删除引用工具名 (调整工具名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void DeleteToolConfigName(string _strCurrentStation, string _strDeleteToolName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                if (toolInputValue.Value.InPutResources.GetWorkName== _strCurrentStation&&
                    toolInputValue.Value.InPutResources.GetToolName == _strDeleteToolName)
                {
                    toolInputValue.Value.InPutResources.GetToolName = "";
                    toolInputValue.Value.InPutResources.GetValueName = "";
                    toolInputValue.Value.InPutResources.UsedValueName = "";
                    toolInputValue.Value.bMark = false;
                }
            }
        }
        #endregion


        #region ModifyComConfigName 调整引用通讯名 (调整通讯名的时候调用)
        /// <summary>
        /// 调整引用通讯名 (调整通讯名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void ModifyComConfigName(string _strNewComName, string _strOldComName)
        {
         
        }
        #endregion

        #region DeleteComConfigName 删除引用通讯名 (调整通讯名的时候调用)
        /// <summary>
        /// 删除引用通讯名 (调整通讯名的时候调用)
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void DeleteComConfigName(string _strDeleteComName)
        {
          
        }
        #endregion


        #region ModifyValueConfigName 调整引用获取值(输出值改变)   反调引用

        /// <summary>
        /// 调整引用获取值(输出值改变)   反调引用
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void ModifyValueConfigName(string _strCurrentStation, string _strOldToolName, string _strNewValueName, string _strOldValueName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                if (toolInputValue.Value.InPutResources.GetWorkName == _strCurrentStation &&
                    toolInputValue.Value.InPutResources.GetToolName == _strOldToolName &&
                    toolInputValue.Value.InPutResources.GetValueName == _strOldValueName)
                {
                    toolInputValue.Value.InPutResources.GetValueName = _strNewValueName;
                }
            }
        }
        #endregion

        #region DeleteValueConfigName 删除引用获取值(输出值改变)   反调引用
        /// <summary>
        /// 删除引用获取值(输出值改变)   反调引用
        /// </summary>
        /// <param name="_strNewStationName"></param>
        /// <param name="_strOldStationName"></param>
        public virtual void DeleteValueConfigName(string _strDeleteValueName)
        {
            foreach (KeyValuePair<string, ToolInputValue> toolInputValue in m_ToolInputValue)
            {
                //if (toolInputValue.Value.InPutResources.GetWorkName == _strCurrentStation &&
                //    toolInputValue.Value.InPutResources.GetToolName == _strOldToolName &&
                //    toolInputValue.Value.InPutResources.GetValueName == _strOldValueName)
                //{
                //    //toolInputValue.Value.InPutResources.GetValueName = "";
                //}
            }
        }
        #endregion


        #region ModifyInPutValueName 调整引用输入值名称 类内输入值被再次调用时引用
        /// <summary>
        /// 调整引用输入值名称 类内输入值被再次调用时引用
        /// </summary>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public virtual void ModifyInPutValueName(string _strNewInputValueName, string _strOldInputValueName)
        {

        }
        #endregion

        #region  DeleteInPutValueName 删除引用输入值名称 类内输入值被再次调用时引用
        /// <summary>
        /// 删除引用输入值名称 类内输入值被再次调用时引用
        /// </summary>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public virtual void DeleteInPutValueName(String _strDeleteInputValueName)
        {

        }
        #endregion


        #region  UpInputValue 上调
        /// <summary>
        /// 上调
        /// </summary>
        /// <param name="_strStationNmae"></param>
        /// <param name="_StationWork"></param>
        /// <returns></returns>
        public LuxsanBaseHelpLib.ExecuteStatus UpInputValue(string _strmoveInputName)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

            if (m_ToolInputValue.Keys.Contains(_strmoveInputName))
            {

                List<string> listKey = m_ToolInputValue.Keys.ToList();
                List<ToolInputValue> listinputTool = m_ToolInputValue.Values.ToList();

                string key = _strmoveInputName;
                ToolInputValue toolInputValue = m_ToolInputValue[key];

                int index = listKey.IndexOf(key);

                listKey.Remove(key);
                listinputTool.Remove(toolInputValue);

                listKey.Insert(index - 1, key);
                listinputTool.Insert(index - 1, toolInputValue);

                m_ToolInputValue = listinputTool.ToDictionary(input => input.strName);


                executeStatus.bStatus = true;
                executeStatus.strErrorMsg = "";


            }
            else
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "Work is Existed ";
            }

            return executeStatus;
        }
        #endregion

        #region  DownInputValue 下调
        /// <summary>
        /// 上调
        /// </summary>
        /// <param name="_strStationNmae"></param>
        /// <param name="_StationWork"></param>
        /// <returns></returns>
        public LuxsanBaseHelpLib.ExecuteStatus DownInputValue(string _strmoveInputName)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

            if (m_ToolInputValue.Keys.Contains(_strmoveInputName))
            {

                List<string> listKey = m_ToolInputValue.Keys.ToList();
                List<ToolInputValue> listbaseTool = m_ToolInputValue.Values.ToList();

                string key = _strmoveInputName;
                ToolInputValue baseTool = m_ToolInputValue[key];

                int index = listKey.IndexOf(key);

                listKey.Remove(key);
                listbaseTool.Remove(baseTool);

                listKey.Insert(index + 1, key);
                listbaseTool.Insert(index + 1, baseTool);

                m_ToolInputValue = listbaseTool.ToDictionary(basetool => basetool.strName);


                executeStatus.bStatus = true;
                executeStatus.strErrorMsg = "";


            }
            else
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "Work is Existed ";
            }

            return executeStatus;
        }
        #endregion


        private async Task< LuxsanBaseHelpLib.ExecuteStatus> AsyncGetInPutValue(string listkey,ToolInputValue listValue)
        {
            LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();

            string strGetWorkName = listValue.InPutResources.GetWorkName;
            string strGetToolName = listValue.InPutResources.GetToolName;
            string strGetValueName = listValue.InPutResources.GetValueName;
            string strUsedValueName = listValue.InPutResources.UsedValueName;

            try
            {
                mutex.WaitOne();

                if (string.IsNullOrEmpty(strGetWorkName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在工站名为空";
                    return executeStatus;
                }
                else if (string.IsNullOrEmpty(strGetToolName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在工具名为空";
                    return executeStatus;
                }
                else if (string.IsNullOrEmpty(strGetValueName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在变量名为空";
                    return executeStatus;
                }
                else if (string.IsNullOrEmpty(strUsedValueName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值使用变量名为空";
                    return executeStatus;
                }

                else if (!MachineWorkManage.m_stationWork.Keys.Contains(strGetWorkName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在工站名不存在:" + strGetWorkName;
                    return executeStatus;
                }
                else if (!MachineWorkManage.m_stationWork[strGetWorkName].m_mesBaseTool.Keys.Contains(strGetToolName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在工具名不存在:" + strGetWorkName + "," + strGetToolName;
                    return executeStatus;
                }
                else if (!MachineWorkManage.m_stationWork[strGetWorkName].m_mesBaseTool[strGetToolName].m_ToolOutPutValue.Keys.Contains(strGetValueName))
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值所在变量名不存在" + strGetWorkName + "," + strGetToolName + "," + strGetValueName;
                    return executeStatus;
                }
                else if (MachineWorkManage.m_stationWork[strGetWorkName].m_mesBaseTool[strGetToolName].m_ToolOutPutValue[strGetValueName].objValue == null)
                {
                    executeStatus.bStatus = false;
                    executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值为空" + strGetWorkName + "," + strGetToolName + "," + strGetValueName;
                    return executeStatus;
                }


                else if (listkey == strUsedValueName)
                {
                    try
                    {
                        object objValue = MachineWorkManage.m_stationWork[strGetWorkName].m_mesBaseTool[strGetToolName].m_ToolOutPutValue[strGetValueName].objValue;


                        listValue.objValue = objValue;

                        if (this.m_bLogEnable && m_LuxsanToolDelegate != null)
                        {
                            object ostrValue = "";
                            string type = listValue.objValue.GetType().Name.ToString();

                            if (type == LuxsanType.ArrayVarType.LuxsanVarTypeDoubleArray)
                            {
                                ostrValue = string.Join(",", listValue.objValue as double[]);
                            }
                            else if (type == LuxsanType.ArrayVarType.LuxsanVarTypeSingleArray)
                                ostrValue = string.Join(",", listValue.objValue as float[]);
                            else if (type == LuxsanType.ArrayVarType.LuxsanVarTypeInt16Array)
                                ostrValue = string.Join(",", listValue.objValue as Int16[]);
                            else if (type == LuxsanType.ArrayVarType.LuxsanVarTypeInt32Array)
                                ostrValue = string.Join(",", listValue.objValue as Int32[]);
                            else if (type == LuxsanType.ArrayVarType.LuxsanVarTypeBooleanArray)
                                ostrValue = string.Join(",", listValue.objValue as Boolean[]);
                            else if (type == LuxsanType.ArrayVarType.LuxsanVarTypeStringArray)
                                ostrValue = string.Join(",", listValue.objValue as string[]);
                            else
                                ostrValue = listValue.objValue;

                            m_LuxsanToolDelegate("RunningLog", this.m_strLogName, m_strStationName, "【" + this.m_strToolName + "】" + "获取输入值:" + listValue.strName +
                                "[" + type + "]" + "{" + ostrValue + "}");
                        }
                        executeStatus.bStatus = true;
                        executeStatus.strErrorMsg = "";
                    }
                    catch (Exception ex)
                    {
                        listValue.objValue = default;
                        executeStatus.bStatus = false;
                        executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "获取输入值名称失败" + ex.Message;

                    }
                }
            }
            catch (Exception ex)
            {
                executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + " 获取输入值异常 Exception ," + ex.Message;

            }
            finally
            {
                mutex.ReleaseMutex();
            }
            return executeStatus;
        }


        #region GetInPutValue  获得输入值
        /// <summary>
        /// 获得输入值
        /// </summary>
        public virtual LuxsanBaseHelpLib.ExecuteStatus GetInPutValue()
        {
              LuxsanBaseHelpLib.ExecuteStatus executeStatus = new LuxsanBaseHelpLib.ExecuteStatus();
              List<ToolInputValue> listValue = m_ToolInputValue.Values.ToList();

            List<string> listkey = m_ToolInputValue.Keys.ToList();
            if (m_ToolInputValue.Count == 0)
              {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "【" + this.m_strToolName + "】" + "输入值未配置，数量为0";
                return executeStatus;
              }

             Task<LuxsanBaseHelpLib.ExecuteStatus>[] tsaks = new Task<LuxsanBaseHelpLib.ExecuteStatus>[listValue.Count];
            try
            {
                //获得输入值
                for (int i = 0; i < listValue.Count; i++)
                {

                    string key = listkey[i];
                    ToolInputValue toolInputValue = listValue[i];
                    // tsaks[i] =  AsyncGetInPutValue(key, toolInputValue);
                    tsaks[i] = Task.Run(() => AsyncGetInPutValue(key, toolInputValue));
                }
                Task.WhenAll(tsaks);
                var vBackResult = tsaks.FirstOrDefault(n => n.Result.strErrorMsg != "");
                if (vBackResult == null)
                    executeStatus.bStatus = true;
                else
                    executeStatus = vBackResult.Result;
            }
            catch (Exception ex)
            {
                executeStatus.bStatus = false;
                executeStatus.strErrorMsg = "获得输入值异常 Exception-2:" + ex.Message;
            }

            return executeStatus;
        }
        #endregion

        #region ModifiedDisToForm 调整后所有参数显示到界面
        /// <summary>
        /// 调整后所有参数显示到界面
        /// </summary>
        public virtual void ModifiedDisToForm()
        {
          
        }
        #endregion


        #region SaveToolInPutValue  保存InPutValue参数
        /// <summary>
        /// 保存InPutValue参数
        /// </summary>
        public void SaveToolInPutValue()
        {
            string strFilePath = m_strFilePath + "\\" + m_strStationName;

            //判断文件是否存在
            if (!(System.IO.Directory.Exists(strFilePath)))
                System.IO.Directory.CreateDirectory(strFilePath);

            //if (System.IO.File.Exists(strFilePath + "\\ToolInPutValueConfig.ini"))
            //    System.IO.File.Delete(strFilePath + "\\ToolInPutValueConfig.ini");

            List<string> listKey = m_ToolInputValue.Keys.ToList();
            List<ToolInputValue> listValue = m_ToolInputValue.Values.ToList();

            m_iniFile.WtiteString(this.m_strToolName + "_Count", "Count", listValue.Count.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");

            for (int i = 0; i < listValue.Count; i++)
            {
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Name", listKey[i], strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Type", listValue[i].strType, strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Resource_GetWorkName", listValue[i].InPutResources.GetWorkName, strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Resource_GetToolId", listValue[i].InPutResources.GetToolName.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Resource_GetValueId", listValue[i].InPutResources.GetValueName.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "Resource_UsedValueId", listValue[i].InPutResources.UsedValueName.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");
                m_iniFile.WtiteString(this.m_strToolName + i.ToString(), "bMark", listValue[i].bMark.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");

                //m_iniFile.WtiteString(this + i.ToString(), "Value", listValue[i].objValue.ToString(), strFilePath + "\\ToolInPutValueConfig.ini");
            }
        }
        #endregion

        #region  LoadToolInPutValue 加载InPutValue参数
        /// <summary>
        /// LoadToolInPutValue 加载InPutValue参数
        /// </summary>
        public void LoadToolInPutValue()
        {
            if (this.m_strToolName == "文件操作12")
            {

            }

            string  strFilePath = m_strFilePath + "\\" + m_strStationName;

            //判断文件是否存在
            if (!(System.IO.Directory.Exists(strFilePath)))
                System.IO.Directory.CreateDirectory(strFilePath);

            int iCount = m_iniFile.GetInt(this.m_strToolName + "_Count", "Count", strFilePath + "\\ToolInPutValueConfig.ini");

            for (int i = 0; i < iCount; i++)
            {
                string _strKey = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Name", strFilePath + "\\ToolInPutValueConfig.ini");
                string _strType = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Type", strFilePath + "\\ToolInPutValueConfig.ini");
                string _strGetWorkName = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Resource_GetWorkName", strFilePath + "\\ToolInPutValueConfig.ini");
                string _iGetToolId = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Resource_GetToolId", strFilePath + "\\ToolInPutValueConfig.ini");
                string _iGetValueId = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Resource_GetValueId", strFilePath + "\\ToolInPutValueConfig.ini");
                string _iUsedValueId = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Resource_UsedValueId", strFilePath + "\\ToolInPutValueConfig.ini");
                string _strValue = m_iniFile.GetString(this.m_strToolName + i.ToString(), "Value", strFilePath + "\\ToolInPutValueConfig.ini");
                bool _bMark = false;
                try
                { _bMark = bool.Parse(m_iniFile.GetString(this.m_strToolName + i.ToString(), "bMark", strFilePath + "\\ToolInPutValueConfig.ini")); }
                catch { }
                if (!m_ToolInputValue.Keys.Contains(_strKey) &&
                    !string.IsNullOrEmpty(_strKey))
                {
                    ToolInputValue toolInputValue = new ToolInputValue();
                    toolInputValue.strName = _strKey;
                    toolInputValue.strType = _strType;
                    toolInputValue.InPutResources.GetWorkName = _strGetWorkName;
                    toolInputValue.InPutResources.GetToolName = _iGetToolId;
                    toolInputValue.InPutResources.GetValueName = _iGetValueId;
                    toolInputValue.InPutResources.UsedValueName = _iUsedValueId;
                    toolInputValue.bMark = _bMark;
                    toolInputValue.objValue = _strValue;
                    m_ToolInputValue.Add(_strKey, toolInputValue);
                }
            }
        }
        #endregion

      

        public virtual long GetRunTimer()
        {
            long ltimer = m_stopwatchStart.ElapsedMilliseconds;
            return ltimer;
        }
        public virtual long GetRunTimer_1()
        {
            long ltimer = m_stopwatchToolRunTimerAll.ElapsedMilliseconds;
            return ltimer;
        }

        

        public virtual void ClearRunTimer()
        {
            m_stopwatchStart.Stop();
            m_stopwatchToolRunTimerAll.Stop();
            m_stopwatchStart.Reset();
            m_stopwatchToolRunTimerAll.Reset();
        }


    }
}


